﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace ChequePrinter.WCF
{
    public class DAL
    {

        public List<Cheque> GetCheques()
        {
            using (var dc = new ObjectModelDataContext())
            {
                return dc.Cheques.ToList();
            }

        }

        public int InsertCheque(Cheque cheque )
        {
            using (var dc = new ObjectModelDataContext())
            {
                dc.Cheques.InsertOnSubmit(cheque);
                dc.SubmitChanges();
                return cheque.Id;
            }
        }

        public void UpdateCheque(Cheque cheque)
        {
            using (var dc = new ObjectModelDataContext())
            {
                Cheque existingCheque = dc.Cheques.FirstOrDefault(p => p.Id == cheque.Id);
                if (existingCheque == null) //check does not exisit
                {
                    
                }
                else
                {
                    existingCheque.RoutingNumber = cheque.RoutingNumber;
                    existingCheque.CustomerName = cheque.CustomerName;
                    existingCheque.CustomerAddress1 = cheque.CustomerAddress1;
                    existingCheque.CustomerAddress2 = cheque.CustomerAddress2;
                    existingCheque.CustomerAddress3 = cheque.CustomerAddress3;
                    existingCheque.CustomerAddress4 = cheque.CustomerAddress4;
                    existingCheque.AccountNumber = cheque.AccountNumber;
                    existingCheque.BankName = cheque.BankName;
                    existingCheque.BankAddress1 = cheque.BankAddress1;
                    existingCheque.BankAddress2 = cheque.BankAddress2;
                    existingCheque.BankAddress3 = cheque.BankAddress3;
                    existingCheque.RoutingNumber = cheque.RoutingNumber;
                }
                dc.SubmitChanges();
                
            }
        }

        public void DeleteCheque(int id)
        {
            using (var dc = new ObjectModelDataContext())
            {
                Cheque existingCheque = dc.Cheques.FirstOrDefault(p => p.Id == id);
                if (existingCheque == null) //check does not exisit
                {

                }
                else
                {
                    dc.Cheques.DeleteOnSubmit(existingCheque);
                    dc.SubmitChanges();
                }
            }
            

        }


    }
}
